﻿using System;
using System.Windows;

using SBPweb.RIA.Shared.Contracts.Controllers;
using SBPweb.RIA.Shared.Contracts.Models;

using SBPweb.RIA.Client.Contracts.Controllers;
using SBPweb.RIA.Client.Contracts.Models;

using SBPweb.RIA.Client.Controls.Base;

using SBPweb.PROvibe.Client.Library.Controllers;
using SBPweb.PROvibe.Client.Library.Models;

using SBPweb.PROvibe.Shared.Library.Components;

namespace SBPweb.PROvibe.Client.Library.Base
{

    public class ClientContext
    {

        #region Fields

        private static object lh = new object();
        private static IConfigurationController configurationController;
        private static IContentContextController contentContextController;

        #endregion

        #region Constructor logic

        internal ClientContext(Type instanceType)
        {
            if (ExtendableApplication.Current == null || ExtendableApplication.Current.IsDesignTime)
            {
                return;
            }
            (ContentContextModel as ContentContextModelBase).SetCurrentModuleByInstance(instanceType);
        }

        internal ClientContext(IClientModule currentModule)
        {
            if (ExtendableApplication.Current == null || ExtendableApplication.Current.IsDesignTime)
            {
                return;
            }
            if (ExtendableApplication.Current.RootVisual != null)
            {
                Dispatcher = ExtendableApplication.Current.RootVisual.Dispatcher;
            }
            (ContentContextModel as ContentContextModelBase).CurrentModule = currentModule;
        }

        #endregion

        #region Properties

        public System.Windows.Threading.Dispatcher Dispatcher
        {
            get;
            private set;
        }

        public INavigationController NavigationController
        {
            get
            {
                return RIA.Client.Controls.Controllers.NavigationController.Controller;
            }
        }

        public INavigationModel NavigationModel
        {
            get
            {
                return NavigationController.Model;
            }
        }

        public IPackageLoaderController PackageLoaderController
        {
            get
            {
                return RIA.Client.Controls.Controllers.PackageLoaderController.Controller;
            }
        }

        public IPackageLoaderModel PackageLoaderModel
        {
            get
            {
                return PackageLoaderController.Model;
            }
        }

        public IConfigurationController ConfigurationController
        {
            get
            {
                lock (lh)
                {
                    if (ExtendableApplication.Current == null || ExtendableApplication.Current.IsDesignTime)
                    {
                        return null;
                    }
                    if (configurationController == null)
                    {
                        string key = typeof(IConfigurationController).FullName;
                        configurationController = ExtendableApplication.Current.Get<IConfigurationController>(key);
                    }
                    return configurationController;
                }
            }
        }

        public IConfigurationModel ConfigurationModel
        {
            get
            {
                return ConfigurationController.Model;
            }
        }

        public IContentContextController ContentContextController
        {
            get
            {
                lock (lh)
                {
                    if (ExtendableApplication.Current == null || ExtendableApplication.Current.IsDesignTime)
                    {
                        return null;
                    }
                    if (contentContextController == null)
                    {
                        string key = typeof(IContentContextController).FullName;
                        contentContextController = ExtendableApplication.Current.Get<IContentContextController>(key);
                    }
                    return contentContextController;
                }
            }
        }

        public IContentContextModel ContentContextModel
        {
            get
            {
                return ContentContextController.Model;
            }
        }

        public IShellContextModel ShellContextModel
        {
            get
            {
                return ShellContext.SingletonInstance;
            }
        }

        public IDOMContextModel BrowserContextModel
        {
            get
            {
                return RIA.Client.Controls.Models.DOMContextModel.SingletonInstance;
            }
        }

        #endregion

    }

}